﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SIG.Model;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using System.Linq.Expressions;
using SIG.Infrastructure.Cache;
using SIG.Resources;
using SIG.Infrastructure.Configs;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class LogService : EntityService<Log>, ILogService
    {
        #region Fields
        private ILogRepository _logRepository;
        private ILoggingService _logger;
        private ICacheService _cacheService;
        #endregion
        public LogService(ILogRepository logRepository, ICacheService cacheService, ILoggingService logger)
            : base(logRepository, cacheService, logger)
        {
            if (logRepository == (ILogRepository)null)
                throw new ArgumentNullException("logRepository");
            if (logger == (ILoggingService)null)
                throw new ArgumentNullException("logger");
            if (cacheService == (ICacheService)null)
                throw new ArgumentNullException("cacheService");
        
            this._logger = logger;
            this._cacheService = cacheService;
            this._logRepository = logRepository;
           
        }


        public int SearchLogsCount(DateTime? startDate, DateTime? expireDate, string level)
        {

            var key = string.Format("{0}s_Search_{1}_{2}_{3}", EntityNames.Log, startDate, expireDate, level);

            int logsCount;

            if (SettingsManager.Log.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    logsCount = (int)_cacheService.Get(key);
                }
                else
                {                   

                    logsCount = _logRepository.SearchLogsCount(startDate, expireDate, level);
                    _cacheService.Set(key, logsCount, 30);

                }
            }
            else
            {
                logsCount = _logRepository.SearchLogsCount(startDate, expireDate, level);
            }


            return logsCount;
         
        }


        public IEnumerable<Log> SearchLogs(int pageIndex, int pageSize, DateTime? startDate, DateTime? expireDate, string level)
        {

            var key = string.Format("{0}s_Search_{1}_{2}_{3}_{4}_{5}", EntityNames.Log, pageIndex, pageSize, startDate, expireDate,
                level);

            List<Log> logs;
            if (SettingsManager.Log.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    logs = (List<Log>)_cacheService.Get(key);

                }
                else
                {
                    logs = _logRepository.SearchLogs(pageIndex, pageSize, startDate, expireDate, level).ToList();
                    _cacheService.Set(key, logs, SettingsManager.Log.CacheDuration);
                }
            }
            else
            {
                logs = _logRepository.SearchLogs(pageIndex, pageSize, startDate, expireDate, level).ToList();
            }

            return logs;
          
        }




        public int RemoveAll()
        {
            int count = 0;
            try
            {
                count = _logRepository.RemoveAll();
                if (count > 0)
                {
                    _cacheService.Invalidate(EntityNames.Log);
                    _logger.Info(string.Format(Logs.ClearAllMessage,EntityNames.Log));
                }
                return count;

            }
            catch (Exception er)
            {
                _logger.Error(string.Format(Logs.ClearAllMessage, EntityNames.Log), er);
            }
            return count;

        }



    }
}
